package com.zk.algorithm.string;

import com.zk.algorithm.Utils;
import com.zk.algorithm.annotation.JianZhiOffer;

/**
 * 剑指 Offer 46. 数字翻译成字符串，有多少种翻译解法
 *
 * {@link DecodeWays}
 *
 * 0 => a
 * 1 => b
 * 2 => c
 * ...
 * 25 => z
 */
@JianZhiOffer
public class TransNumToString {

    public static void main(String...args) {
        Utils.println(new TransNumToString().getTransCount("12258"));
    }

    public int getTransCount(int number) {
        if (number < 0) {
            return 0;
        }

        return getTransCount(String.valueOf(number));
    }

    private int getTransCount(String number) {
        int count = 0;
        int[] counts = new int[number.length()];

        for (int i = number.length() - 1; i >= 0; i--) {
            if (i < number.length() - 1) {
                count = counts[i + 1];
            } else {
                count = 1;
            }

            if (i < number.length() - 1) {
                int digit1 = number.charAt(i) - '0';
                int digit2 = number.charAt(i + 1) - '0';
                int converted = digit1 * 10 + digit2;

                if (converted >= 10 && converted <= 25) {
                    if (i < number.length() - 2) {
                        count += counts[i + 2];
                    } else {
                        count += 1;
                    }
                }
            }

            counts[i] = count;
        }

        return counts[0];
    }

}
